package com.hippo.core.ui.compose.setting

import LoadingDialog
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.hippo.core.R
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.core.ext.toMd5
import com.hippo.core.state.DialogType
import com.hippo.core.ui.components.CommonBody
import com.hippo.core.ui.components.ConfirmAlertDialog
import com.hippo.core.ui.components.InputDialog
import com.hippo.core.ui.components.TextLayout
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.components.VerticalSpacer
import com.hippo.core.ui.navigation.AppRoute
import com.hippo.core.ui.theme.HippoTheme
import com.hippo.core.vm.SettingVM

@Composable
fun SettingScreen(navController: NavController, vm: SettingVM = viewModel()) {

    val showDeleteAccountDialog = remember{ mutableStateOf(false) }

    val uiState = vm.uiState

    if(uiState.dialogType == DialogType.LoadingDialog()){
        LoadingDialog(
            onDismissRequest = {
                vm.dismissDialog()
            }
        )
    }

    if(showDeleteAccountDialog.value){
        DeleteAccountDialog(
            oldEmail = uiState.userEmail.orEmpty(),
            onDelete = {
                vm.userOffPC(it.toMd5()) {
                    navController.navigate(AppRoute.LOGIN){
                        popUpTo(AppRoute.LOGIN){
                            inclusive = true
                        }
                    }
                }
            },
            onDismissRequest = {
                showDeleteAccountDialog.value = false
            }
        )
    }

   CommonBody(
        title = stringResource(R.string.setting_1),
        onBack = {
            navController.popBackStack()
        }
    ) {

        Column(
            modifier = Modifier
                .fillMaxSize()
                .paddingBody()
        ) {

            TextLayout(text = stringResource(R.string.setting_2)){
                navController.navigate(AppRoute.AccountScreen)
            }
            TextLayout(text = stringResource(R.string.setting_3)){
                navController.navigate(AppRoute.MessageSettingScreen)
            }
            TextLayout(text = stringResource(R.string.setting_4)){
                 navController.navigate(AppRoute.LanguageScreen)
            }
            TextLayout(text = stringResource(R.string.setting_5)){
                 navController.navigate(AppRoute.DeviceSettingScreen)
            }
           TextLayout(text = stringResource(R.string.setting_6)){
               showDeleteAccountDialog.value = true
            }


            VerticalSpacer(36.dp, modifier = Modifier.weight(1F))

            ThemeButton(
                modifier = Modifier.fillMaxWidth(),
                text = stringResource(R.string.setting_7),
                onClick = {
                    vm.logout {
                        navController.navigate(AppRoute.LOGIN){
                            popUpTo(AppRoute.LOGIN){
                                inclusive = true
                            }
                        }
                    }
                }
            )

            VerticalSpacer(36.dp)

        }

    }

}


@Preview(showBackground = true)
@Composable
private fun SLanguageScreenPreview() {
    HippoTheme {
        SettingScreen(
            navController = rememberNavController()
        )
    }
}